package com.jzcn.admin.controller.system;

import com.jzcn.admin.base.BaseController;
import com.jzcn.common.dto.system.tool.SearchToolDTO;
import com.jzcn.common.dto.system.tool.ToolCategoryRecordDTO;
import com.jzcn.common.dto.system.tool.ToolListDTO;
import com.jzcn.common.dto.system.tool.ToolRecordDTO;
import com.jzcn.common.response.ResponseResult;
import com.jzcn.common.utils.JzPage;
import com.jzcn.service.AppToolCategoryService;
import com.jzcn.service.AppToolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "Tool", tags = "工具管理")
@RequestMapping("/api/tool")
public class ToolController extends BaseController {

    @Resource
    AppToolService appToolService;

    @Resource
    AppToolCategoryService appToolCategoryService;

    @ApiOperation("工具分页列表")
    @GetMapping("/page")
    @RequiresPermissions("app:tool:page")
    public ResponseResult<JzPage<ToolListDTO>> page(SearchToolDTO dto) {
        return ResponseResult.success(appToolService.listPage(dto));
    }

    @ApiOperation("新增")
    @PostMapping("/create")
    @RequiresPermissions("app:tool:add")
    public ResponseResult<Boolean> create(@RequestBody ToolRecordDTO dto) {
        dto.setCreateUserId(getUserId());
        return ResponseResult.success(appToolService.create(dto));
    }

    @ApiOperation("更新")
    @PutMapping("/update/{id}")
    @RequiresPermissions("app:tool:update")
    public ResponseResult<Boolean> update(@RequestBody ToolRecordDTO dto,@PathVariable("id") Integer id) {
        dto.setId(id);
        return ResponseResult.success(appToolService.create(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("app:tool:delete")
    public ResponseResult<Boolean> delete(@PathVariable("id") Integer id) {
        return ResponseResult.success(appToolService.delete(id));
    }


    @ApiOperation("分类")
    @GetMapping("/category/list")
    @RequiresPermissions("app:tool:category:list")
    public ResponseResult<List<ToolCategoryRecordDTO>> list() {
        return ResponseResult.success(appToolCategoryService.listData());
    }

    @ApiOperation("新增分类")
    @PostMapping("/category/create")
    @RequiresPermissions("app:tool:category:add")
    public ResponseResult<Boolean> categoryCreate(@RequestBody ToolCategoryRecordDTO dto) {
        return ResponseResult.success(appToolCategoryService.create(dto));
    }

    @ApiOperation("更新分类")
    @PutMapping("/category/update/{id}")
    @RequiresPermissions("app:tool:category:update")
    public ResponseResult<Boolean> categoryUpdate(@RequestBody ToolCategoryRecordDTO dto,@PathVariable("id") Integer id) {
        dto.setId(id);
        return ResponseResult.success(appToolCategoryService.update(dto));
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/category/delete/{id}")
    @RequiresPermissions("app:tool:category:delete")
    public ResponseResult<Boolean> categoryDelete(@PathVariable("id") Integer id) {
        return ResponseResult.success(appToolCategoryService.delete(id));
    }
}
